package inits

import (
	AppConf "examination-management/api/cmd/config"
	"github.com/google/uuid"
	"github.com/hashicorp/consul/api"
	"github.com/spf13/viper"
	"log"
)

func init() {
	InitConfig()
}
func InitConfig() {
	config := api.DefaultConfig()
	config.Address = "14.103.243.15:8500"
	client, _ := api.NewClient(config)
	NewClient := new(api.AgentServiceRegistration)
	ClientId := uuid.NewString()
	NewClient.ID = ClientId
	NewClient.Name = AppConf.AppConf.Name
	NewClient.Tags = []string{"demo", "management"}
	NewClient.Port = AppConf.AppConf.Port
	NewClient.Address = AppConf.AppConf.Host
	err := client.Agent().ServiceRegister(NewClient)
	if err != nil {
		log.Println(err)
		return
	}
	log.Println("consul 连接成功")

	viper.SetConfigFile("D:\\goWork\\src\\examination-management\\api\\dev.yaml")
	err = viper.ReadInConfig()
	if err != nil {
		panic("viper 配置读取失败")
	}
	log.Println("viper 配置读取成功")
	err = viper.Unmarshal(&AppConf.AppConf)
	if err != nil {
		panic("viper 配置解析失败")
	}
	log.Println("viper 配置解析成功", AppConf.AppConf)
}
